package com.chaos.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.chaos.system.api.data.RbacPermissionMenuBtnData;
import com.chaos.system.entity.RbacPermissionMenuBtn;
import com.chaos.system.mapper.RbacPermissionMenuBtnMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@Component
public class RbacPermissionMenuBtnService extends ServiceImpl<RbacPermissionMenuBtnMapper, RbacPermissionMenuBtn> {

    public ID insertModel(RbacPermissionMenuBtnData data) {
        RbacPermissionMenuBtn entity = ConvertHelper.convert(data, RbacPermissionMenuBtn.class);
        return save(entity) ? new ID(entity.getId()) : null;
    }

    public boolean deleteModel(ID data) {
        return removeById(data.getId());
    }

    public boolean updateModelByID(RbacPermissionMenuBtnData data) {
        RbacPermissionMenuBtn entity = ConvertHelper.convert(data, RbacPermissionMenuBtn.class);
        return update(entity, new UpdateWrapper<RbacPermissionMenuBtn>().eq(Table.ID, data.getId()));
    }

    public RbacPermissionMenuBtnData selectByID(ID data) {
        return ConvertHelper.convert(getOne(new QueryWrapper<RbacPermissionMenuBtn>().eq(Table.ID, data.getId())),
                RbacPermissionMenuBtnData.class);
    }

    public List<RbacPermissionMenuBtnData> selectByData(RbacPermissionMenuBtnData data) {
        LambdaQueryWrapper<RbacPermissionMenuBtn> query = new LambdaQueryWrapper<>();
        query.eq(ObjectUtil.isNotNull(data.getMenuOrBtnId()), RbacPermissionMenuBtn::getMenuOrBtnId, data.getMenuOrBtnId());
        query.orderByDesc(RbacPermissionMenuBtn::getId);
        return ConvertHelper.convert(list(query), RbacPermissionMenuBtnData.class);
    }

    public PageList<RbacPermissionMenuBtnData> selectByPage(PageQueryDto<RbacPermissionMenuBtnData> pageData) {
        QueryWrapper<RbacPermissionMenuBtn> query = new QueryWrapper();
        query.orderByDesc(Table.ID);
        return PageHelper.pageList(page(PageHelper.page(pageData), query), RbacPermissionMenuBtnData.class);
    }

}
